﻿using System;
using System.Windows;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using Creventive.Wpf.ShellFactory.Services;
using Creventive.Wpf.ShellFactory.Workspace;
using Microsoft.Win32;
using MultiMasterView.Modules.Core.ImageViewer;
using MultiMasterView.Modules.Core.ImageViewer.RuntimeModels;

[assembly: ApplicationViewModel(typeof(ImageViewerViewModel), typeof(IImageViewerViewModel))]

namespace MultiMasterView.Modules.Core.ImageViewer.RuntimeModels
{
    internal class ImageViewerViewModel : ApplicationViewModel, IImageViewerViewModel
    {
        private ModelCommand openCommand;
        private ImageSource image = null;

        public ImageViewerViewModel(IServiceContainer services)
            : base(services, ApplicationViewModelFlags.None)
        {
            this.openCommand = new ModelCommand(this.Open);
        }

        public ImageSource Image
        {
            get { return this.image; }
            private set
            {
                if (this.image == value)
                    return;
                this.image = value;
                this.OnImageChanged();
            }
        }

        public ICommand OpenCommand
        {
            get { return this.openCommand; }
        }

        private void Open()
        {
            // In a real-life application I would encapsulate this dialog into a generic service in the ServiceContainer.
            OpenFileDialog openFileDialog = new OpenFileDialog();
            openFileDialog.Filter = "Images|*.jpg;*.jpeg;*.png;*.gif";
            if (openFileDialog.ShowDialog(Application.Current.MainWindow) == true)
                this.Image = new BitmapImage(new Uri(openFileDialog.FileName));
        }

        protected virtual void OnImageChanged()
        {
            this.OnPropertyChanged("Image");
        }
    }
}